package com.hiyee.utils;

import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.temporal.ChronoUnit;
import java.util.Date;

/**
 * 日期时间工具类
 *
 * @author WangBin
 */
public class DateTimeUtil {
    /**
     * 获取指定时间的时间戳
     *
     * @param Date date
     * @return Long
     */
    public static Long getCurrentStamp(Date date) {
        if(date == null) {
            date = new Date();
        }
        return Long.valueOf(String.valueOf(date.getTime()/1000));
    }

    /**
     * 获取当前指定格式日期
     *
     * @param String format
     * @return String
     */
    public static String getDateTimeStr(String format) {
        Date d = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat(format); // "yyyy-MM-dd HH:mm:ss"
        return sdf.format(d);
    }

    /**
     * 获取当天距离结束剩余的秒数
     *
     * @param Date currentDate
     * @return Integer
     */
    public static Integer getRemainSecondsOneDay(Date currentDate) {
        LocalDateTime midnight = LocalDateTime.ofInstant(currentDate.toInstant(),
                        ZoneId.systemDefault()).plusDays(1).withHour(0).withMinute(0)
                .withSecond(0).withNano(0);
        LocalDateTime currentDateTime = LocalDateTime.ofInstant(currentDate.toInstant(),
                ZoneId.systemDefault());
        long seconds = ChronoUnit.SECONDS.between(currentDateTime, midnight);
        return (int) seconds;
    }
}
